import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'dart:async';
import 'package:path_provider/path_provider.dart';
import 'package:flutter_qt_7ze/flutter_qt_7ze.dart' as flutter_qt_7ze;
import 'package:path/path.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  int sumResult = 0;
   Future<int> sumAsyncResult = Future((){return 1;});

  void init() async {
    // 加载assets资源
    // var ass = await rootBundle.load('assets/sample.7z');
    // Uint8List bytes = ass.buffer.asUint8List();

    var documents = await getApplicationDocumentsDirectory();
    // String path = join(documents.path, "sample.7z");
    // var tmpFile = File(path);
    // if (tmpFile.existsSync()) {
    //   tmpFile.deleteSync();
    // }
    // tmpFile
    //   ..createSync(recursive: true) // 同步创建文件
    //   ..writeAsBytesSync(bytes);

    flutter_qt_7ze.extractFile(fileName:  'flutter_assets/assets/sample.7z', desPath:  documents.path, isAndroidAssets: true);

    // var ptr = flutter_qt_7ze.initQt7ze();
    // flutter_qt_7ze.open7zFile(ptr, path);
    // var fileNum = flutter_qt_7ze.qt7ZFileNum(ptr);
    // for(int i = 0; i < fileNum; ++i){
    //   var fileName = flutter_qt_7ze.qt7ZFileName(ptr, i);
    //   flutter_qt_7ze.qt7ZExtract(ptr, i, join(documents.path, fileName));
    // }
  }

  @override
  void initState() {
    super.initState();
    // sumResult = flutter_qt_7ze.sum(1, 2);
    // sumAsyncResult = flutter_qt_7ze.sumAsync(3, 4);
    init();
  }

  @override
  Widget build(BuildContext context) {
    const textStyle = TextStyle(fontSize: 25);
    const spacerSmall = SizedBox(height: 10);
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Native Packages'),
        ),
        body: SingleChildScrollView(
          child: Container(
            padding: const EdgeInsets.all(10),
            child: Column(
              children: [
                const Text(
                  'This calls a native function through FFI that is shipped as source in the package. '
                  'The native code is built as part of the Flutter Runner build.',
                  style: textStyle,
                  textAlign: TextAlign.center,
                ),
                spacerSmall,
                Text(
                  'sum(1, 2) = $sumResult',
                  style: textStyle,
                  textAlign: TextAlign.center,
                ),
                spacerSmall,
                FutureBuilder<int>(
                  future: sumAsyncResult,
                  builder: (BuildContext context, AsyncSnapshot<int> value) {
                    final displayValue =
                        (value.hasData) ? value.data : 'loading';
                    return Text(
                      'await sumAsync(3, 4) = $displayValue',
                      style: textStyle,
                      textAlign: TextAlign.center,
                    );
                  },
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
